package com.community.content;

import java.io.IOException;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import com.example.logindesign.Mainmenu;
import com.example.logindesign.R;
import com.spinner.keyvalue.SpinnerClass;

import android.R.integer;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class CommunityStatisticsKuiwenActivity extends Activity {
	private static final String NAMESPACE = "http://tempuri.org/";
	private static final String ENDPOINT = "http://192.168.1.198:1536/CommunitySystemsicWsWF.asmx";
	private String methodName, soapAcion;

	// 为了判定当前选到的那个下拉框
	private String spinnerLocation;
	// 3个webservice参数
	private String unitcode, xiaoqu, lou;
	// 为了初始化下拉列表用的几个Unitcode
	private String myUnitcode, shiUnitcode, xianUnitcode, jiedaoUnitcode,
			shequUnitcode;
	// 返回按钮
	private ImageButton btnBack;
	// 36个TextView
	// 户籍统计
	private TextView zhsTextView;
	// 人口统计
	private TextView czrksTextView, zzrksTextView, kghrksTextView,
			jghrksTextView;
	// 性别
	private TextView nanTextView, nvTextView;
	// 文化程度
	private TextView bswhTextView, sswhTextView, yjswhTextView, bkwhTextView,
			dzwhTextView, gzwhTextView, zzwhTextView, czwhTextView,
			xxwhTextView, qtwhTextView;
	// 政治面貌
	private TextView dyTextView, ybdyTextView, tyTextView, qzTextView,
			mzdTextView;
	// 婚姻状况
	private TextView dsTextView, yhTextView, lyTextView, soTextView;
	// 其他
	private TextView jhsyTextView, xgTextView, dbTextView, cjTextView,
			ltxTextView, ftjrTextView, pkhTextView, gglrTextView, ldrkTextView,
			ylbxTextView;
	// TextView 的值
	// 户籍统计
	private String zhsValue = "";
	// 人口统计
	private String czrksValue = "", zzrksValue = "", kghrksValue = "",
			jghrksValue = "";
	// 性别
	private String nanValue = "", nvValue = "";
	// 文化程度
	private String bswhValue = "", sswhValue = "", yjswhValue = "",
			bkwhValue = "", dzwhValue = "", gzwhValue = "", zzwhValue = "",
			czwhValue = "", xxwhValue = "", qtwhValue = "";
	// 政治面貌
	private String dyValue = "", ybdyValue = "", tyValue = "", qzValue = "",
			mzdValue = "";
	// 婚姻状况
	private String dsValue = "", yhValue = "", lyValue = "", soValue = "";
	// 其他
	private String jhsyValue = "", xgValue = "", dbValue = "", cjValue = "",
			ltxValue = "", ftjrValue = "", pkhValue = "", gglrValue = "",
			ldrkValue = "", ylbxValue = "";

	// 6个下拉列表
	private Spinner shiSpinner;
	private Spinner xianSpinner;
	private Spinner jiedaoSpinner;
	private Spinner shequSpinner;
	private Spinner xiaoquSpinner;
	private Spinner louSpinner;
	private ArrayList<SpinnerClass> shiSpinnerArrayList;
	private ArrayAdapter<SpinnerClass> shiSpinnerAdapter;
	private ArrayList<SpinnerClass> xianSpinnerArrayList;
	private ArrayAdapter<SpinnerClass> xianSpinnerAdapter;
	private ArrayList<SpinnerClass> jiedaoSpinnerArrayList;
	private ArrayAdapter<SpinnerClass> jiedaoSpinnerAdapter;
	private ArrayList<SpinnerClass> shequSpinnerArrayList;
	private ArrayAdapter<SpinnerClass> shequSpinnerAdapter;
	private ArrayList<SpinnerClass> xiaoquSpinnerArrayList;
	private ArrayAdapter<SpinnerClass> xiaoquSpinnerAdapter;
	private ArrayList<SpinnerClass> louSpinnerArrayList;
	private ArrayAdapter<SpinnerClass> louSpinnerAdapter;

	// 搜索按钮
	private Button btnSearch;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_community_statistics_kuiwen);
		// sharedpreference中取得相应值
		getValueFromSharedPreferences();
		// 绑定textview 及 backbutton 按钮
		initTextViewEtc();
		// 取当前unitcode的上级和本级unitcode
		getMyUnitcodeAbout();
	}

	/**
	 * 初始化下拉列表用的几个unitcode
	 */
	private void getMyUnitcodeAbout() {
		new getMyUnitcodeAboutTask().execute();
	}

	/**
	 * 得到详细信息
	 * 
	 * @return
	 */
	private String getData() {
		// TODO Auto-generated method stub
		String resultString = "";
		String methodName = "get_CommunityStatistics";
		String soapAcion = "http://tempuri.org/get_CommunityStatistics";
		SoapObject soapObject = new SoapObject(NAMESPACE, methodName);
		// 操作人员所属区域编码
		// 首次加载自己这一级别的数据
		soapObject.addProperty("unitcode", unitcode);
		soapObject.addProperty("qu", xiaoqu);
		soapObject.addProperty("lou", lou);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.setOutputSoapObject(soapObject);
		envelope.dotNet = true;
		HttpTransportSE transportSE = new HttpTransportSE(ENDPOINT);
		try {
			transportSE.call(soapAcion, envelope);
			if (envelope.getResponse() != null) {
				SoapObject object = (SoapObject) envelope.bodyIn;
				resultString = object.getProperty(0).toString();
				Log.e("webservice取得", resultString);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("IOException", e.getMessage());
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("XmlPullParserException", e.getMessage());
		}
		return resultString;
	}

	/**
	 * 解析json数据返回hashmap格式数据
	 * 
	 * @return
	 */
	private String jsonFormat(String resultString) {
		JSONArray jsonArray;
		try {
			jsonArray = new JSONArray(resultString);
			JSONObject jsonObject = jsonArray.optJSONObject(0);
			if (jsonObject.optString("Code").equals("1")) {
				// 根据实际从第2组开始取得有关数据
				jsonObject = jsonArray.optJSONObject(1);
				zhsValue = jsonObject.optString("sumjmhs");
				zzrksValue = jsonObject.optString("zzrk");
				czrksValue = jsonObject.optString("czrk");
				jghrksValue = jsonObject.optString("jghrk");
				kghrksValue = jsonObject.optString("kghrk");
				nanValue = jsonObject.optString("jmmenrk");
				nvValue = jsonObject.optString("jmwomenrk");
				dyValue = jsonObject.optString("jmdy");
				tyValue = jsonObject.optString("jmty");
				qzValue = jsonObject.optString("jmqz");
				mzdValue = jsonObject.optString("jmmzdp");
				ybdyValue = jsonObject.optString("jmybdy");
				bswhValue = jsonObject.optString("jmbs");
				qtwhValue = jsonObject.optString("jmqt");
				xxwhValue = jsonObject.optString("jmxx");
				czwhValue = jsonObject.optString("jmcz");
				gzwhValue = jsonObject.optString("jmgz");
				sswhValue = jsonObject.optString("jmss");
				zzwhValue = jsonObject.optString("jmzz");
				dzwhValue = jsonObject.optString("jmdz");
				bkwhValue = jsonObject.optString("jmbk");
				yjswhValue = jsonObject.optString("jmyjs");
				dsValue = jsonObject.optString("jmds");
				lyValue = jsonObject.optString("jmly");
				soValue = jsonObject.optString("jmso");
				jhsyValue = jsonObject.optString("jmjs");
				dbValue = jsonObject.optString("jmdbm");
				xgValue = jsonObject.optString("jmxg");
				cjValue = jsonObject.optString("jmcj");
				ltxValue = jsonObject.optString("jmltx");
				ftjrValue = jsonObject.optString("jmftjrl");
				gglrValue = jsonObject.optString("jmgg");
				pkhValue = jsonObject.optString("jmpk");
				ldrkValue = jsonObject.optString("jmldrk");
				ylbxValue = jsonObject.optString("jmylbx");
				return "success";
			} else {
				return "fail";
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "fail";
	}

	/**
	 * sharedpreference中取得相应值
	 */
	private void getValueFromSharedPreferences() {
		// TODO Auto-generated method stub
		SharedPreferences sharedPreferences = getSharedPreferences("UserInfo",
				Activity.MODE_PRIVATE);
		unitcode = sharedPreferences.getString("unitcode", "");
		myUnitcode = sharedPreferences.getString("unitcode", "");
	}

	/**
	 * 绑定textview 及 backbutton 按钮搜索alertdialog
	 */
	private void initTextViewEtc() {
		// TODO Auto-generated method stub
		// findviewbyid
		// 户籍统计
		zhsTextView = (TextView) findViewById(R.id.community_zhsTextView);
		// 人口统计
		czrksTextView = (TextView) findViewById(R.id.community_czrksTextView);
		zzrksTextView = (TextView) findViewById(R.id.community_zzrksTextView);
		kghrksTextView = (TextView) findViewById(R.id.community_kghrksTextView);
		jghrksTextView = (TextView) findViewById(R.id.community_jghrksTextView);
		// 性别
		nanTextView = (TextView) findViewById(R.id.community_nanTextView);
		nvTextView = (TextView) findViewById(R.id.community_nvTextView);
		// 文化程度
		bswhTextView = (TextView) findViewById(R.id.community_bsTextView);
		sswhTextView = (TextView) findViewById(R.id.community_ssTextView);
		yjswhTextView = (TextView) findViewById(R.id.community_yjsTextView);
		bkwhTextView = (TextView) findViewById(R.id.community_bkTextView);
		dzwhTextView = (TextView) findViewById(R.id.community_dzTextView);
		gzwhTextView = (TextView) findViewById(R.id.community_gzTextView);
		zzwhTextView = (TextView) findViewById(R.id.community_zzTextView);
		czwhTextView = (TextView) findViewById(R.id.community_czTextView);
		xxwhTextView = (TextView) findViewById(R.id.community_xxTextView);
		qtwhTextView = (TextView) findViewById(R.id.community_qtwhTextView);
		// 政治面貌
		dyTextView = (TextView) findViewById(R.id.community_dyTextView);
		ybdyTextView = (TextView) findViewById(R.id.community_ybdyTextView);
		tyTextView = (TextView) findViewById(R.id.community_tyTextView);
		qzTextView = (TextView) findViewById(R.id.community_qzTextView);
		mzdTextView = (TextView) findViewById(R.id.community_mzdTextView);
		// 婚姻状况
		dsTextView = (TextView) findViewById(R.id.community_dsTextView);
		yhTextView = (TextView) findViewById(R.id.community_yhTextView);
		lyTextView = (TextView) findViewById(R.id.community_lyTextView);
		soTextView = (TextView) findViewById(R.id.community_soTextView);
		// 其他
		jhsyTextView = (TextView) findViewById(R.id.community_jhsyTextView);
		xgTextView = (TextView) findViewById(R.id.community_xgTextView);
		dbTextView = (TextView) findViewById(R.id.community_dbTextView);
		cjTextView = (TextView) findViewById(R.id.community_cjTextView);
		ltxTextView = (TextView) findViewById(R.id.community_ltxTextView);
		ftjrTextView = (TextView) findViewById(R.id.community_ftjrTextView);
		pkhTextView = (TextView) findViewById(R.id.community_pkhTextView);
		gglrTextView = (TextView) findViewById(R.id.community_gglrTextView);
		ldrkTextView = (TextView) findViewById(R.id.community_ldrkTextView);
		ylbxTextView = (TextView) findViewById(R.id.community_ylbxTextView);

		// setText 耗时的操作放到线程里去
		new setTextViewValueTask().execute();

		// 返回按钮
		btnBack = (ImageButton) findViewById(R.id.btn_community_back);
		btnBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 跳转到menu页面
				skipMainMenuActivity();
			}
		});
		btnSearch = (Button) findViewById(R.id.btn_community_search);
		btnSearch.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 搜索页面
				AlertDialog.Builder builder = new AlertDialog.Builder(
						CommunityStatisticsKuiwenActivity.this);
				View view = View.inflate(
						CommunityStatisticsKuiwenActivity.this,
						R.layout.alertdialog_community_searchdialog, null);

				// findviewbyid
				final TableRow xianTableRow = (TableRow) view
						.findViewById(R.id.tablerow_community_xian);
				final TableRow jiedaoTableRow = (TableRow) view
						.findViewById(R.id.tablerow_community_jiedao);
				final TableRow shequTableRow = (TableRow) view
						.findViewById(R.id.tablerow_community_shequ);
				final TableRow xiaoquTableRow = (TableRow) view
						.findViewById(R.id.tablerow_community_xiaoqu);
				final TableRow louTableRow = (TableRow) view
						.findViewById(R.id.tablerow_community_lou);

				xianTableRow.setVisibility(View.GONE);
				jiedaoTableRow.setVisibility(View.GONE);
				shequTableRow.setVisibility(View.GONE);
				xiaoquTableRow.setVisibility(View.GONE);
				louTableRow.setVisibility(View.GONE);

				shiSpinner = (Spinner) view
						.findViewById(R.id.community_shiSpinner);
				xianSpinner = (Spinner) view
						.findViewById(R.id.community_xianSpinner);
				jiedaoSpinner = (Spinner) view
						.findViewById(R.id.community_jiedaoSpinner);
				shequSpinner = (Spinner) view
						.findViewById(R.id.community_shequSpinner);
				xiaoquSpinner = (Spinner) view
						.findViewById(R.id.community_xiaoquSpinner);
				louSpinner = (Spinner) view
						.findViewById(R.id.community_louSpinner);

				// setadapter
				shiSpinnerArrayList = new ArrayList<SpinnerClass>();
				shiSpinnerArrayList.add(new SpinnerClass("潍坊市", "3707000000"));
				shiSpinnerAdapter = new ArrayAdapter<SpinnerClass>(
						CommunityStatisticsKuiwenActivity.this,
						android.R.layout.simple_spinner_dropdown_item,
						shiSpinnerArrayList);
				shiSpinner.setAdapter(shiSpinnerAdapter);
				shiSpinnerAdapter.notifyDataSetChanged();

				xianSpinnerArrayList = new ArrayList<SpinnerClass>();
				xianSpinnerArrayList.add(new SpinnerClass("请选择", ""));
				xianSpinnerAdapter = new ArrayAdapter<SpinnerClass>(
						CommunityStatisticsKuiwenActivity.this,
						android.R.layout.simple_spinner_dropdown_item,
						xianSpinnerArrayList);
				xianSpinner.setAdapter(xianSpinnerAdapter);
				xianSpinnerAdapter.notifyDataSetChanged();

				jiedaoSpinnerArrayList = new ArrayList<SpinnerClass>();
				jiedaoSpinnerArrayList.add(new SpinnerClass("请选择", ""));
				jiedaoSpinnerAdapter = new ArrayAdapter<SpinnerClass>(
						CommunityStatisticsKuiwenActivity.this,
						android.R.layout.simple_spinner_dropdown_item,
						jiedaoSpinnerArrayList);
				jiedaoSpinner.setAdapter(jiedaoSpinnerAdapter);
				jiedaoSpinnerAdapter.notifyDataSetChanged();

				shequSpinnerArrayList = new ArrayList<SpinnerClass>();
				shequSpinnerArrayList.add(new SpinnerClass("请选择", ""));
				shequSpinnerAdapter = new ArrayAdapter<SpinnerClass>(
						CommunityStatisticsKuiwenActivity.this,
						android.R.layout.simple_spinner_dropdown_item,
						shequSpinnerArrayList);
				shequSpinner.setAdapter(shequSpinnerAdapter);
				shequSpinnerAdapter.notifyDataSetChanged();

				xiaoquSpinnerArrayList = new ArrayList<SpinnerClass>();
				xiaoquSpinnerArrayList.add(new SpinnerClass("请选择", ""));
				xiaoquSpinnerAdapter = new ArrayAdapter<SpinnerClass>(
						CommunityStatisticsKuiwenActivity.this,
						android.R.layout.simple_spinner_dropdown_item,
						xiaoquSpinnerArrayList);
				xiaoquSpinner.setAdapter(xiaoquSpinnerAdapter);
				xiaoquSpinnerAdapter.notifyDataSetChanged();

				louSpinnerArrayList = new ArrayList<SpinnerClass>();
				louSpinnerArrayList.add(new SpinnerClass("请选择", ""));
				louSpinnerAdapter = new ArrayAdapter<SpinnerClass>(
						CommunityStatisticsKuiwenActivity.this,
						android.R.layout.simple_spinner_dropdown_item,
						louSpinnerArrayList);
				louSpinner.setAdapter(louSpinnerAdapter);
				louSpinnerAdapter.notifyDataSetChanged();

				// setOnItemSelected
				louSpinner
						.setOnItemSelectedListener(new OnItemSelectedListener() {

							@Override
							public void onItemSelected(AdapterView<?> parent,
									View view, int position, long id) {
								// TODO Auto-generated method stub
								String selectedValue = ((SpinnerClass) louSpinner
										.getSelectedItem()).getValue();
								lou = selectedValue;
							}

							@Override
							public void onNothingSelected(AdapterView<?> arg0) {
								// TODO Auto-generated method stub

							}
						});
				xiaoquSpinner
						.setOnItemSelectedListener(new OnItemSelectedListener() {

							@Override
							public void onItemSelected(AdapterView<?> parent,
									View view, int position, long id) {
								// TODO Auto-generated method stub
								String selectedValue = ((SpinnerClass) xiaoquSpinner
										.getSelectedItem()).getValue();
								xiaoqu = selectedValue;
								spinnerLocation = "xiaoqu";
								if (spinnerLocation.equals("xiaoqu")) {
									louTableRow.setVisibility(View.VISIBLE);
								} else {
									louTableRow.setVisibility(View.GONE);
								}
								// 填充楼下拉列表数据
								new setlouSpinnerTask().execute();
							}

							@Override
							public void onNothingSelected(AdapterView<?> arg0) {
								// TODO Auto-generated method stub

							}
						});

				shequSpinner
						.setOnItemSelectedListener(new OnItemSelectedListener() {

							@Override
							public void onItemSelected(AdapterView<?> parent,
									View view, int position, long id) {
								// TODO Auto-generated method stub
								String selectedValue = ((SpinnerClass) shequSpinner
										.getSelectedItem()).getValue();
								unitcode = selectedValue;
								louTableRow.setVisibility(View.GONE);
								
								spinnerLocation = "shequ";
								if (spinnerLocation.equals("shequ")) {
									xiaoquTableRow.setVisibility(View.VISIBLE);
								} else {
									xiaoquTableRow.setVisibility(View.GONE);
								}
								// 填充小区下拉列表数据
								new setxiaoquSpinnerTask().execute();
							}

							@Override
							public void onNothingSelected(AdapterView<?> arg0) {
								// TODO Auto-generated method stub

							}
						});

				jiedaoSpinner
						.setOnItemSelectedListener(new OnItemSelectedListener() {

							@Override
							public void onItemSelected(AdapterView<?> parent,
									View view, int position, long id) {
								// TODO Auto-generated method stub
								String selectedValue = ((SpinnerClass) jiedaoSpinner
										.getSelectedItem()).getValue();
								unitcode = selectedValue;
								xiaoquTableRow.setVisibility(View.GONE);
								louTableRow.setVisibility(View.GONE);
								spinnerLocation = "jiedao";
								if (spinnerLocation.equals("jiedao")) {
									shequTableRow.setVisibility(View.VISIBLE);
								} else {
									shequTableRow.setVisibility(View.GONE);
								}
								// 填充街道下拉列表数据
								new setshequSpinnerTask().execute();

							}

							@Override
							public void onNothingSelected(AdapterView<?> arg0) {
								// TODO Auto-generated method stub

							}
						});

				xianSpinner
						.setOnItemSelectedListener(new OnItemSelectedListener() {

							@Override
							public void onItemSelected(AdapterView<?> parent,
									View view, int position, long id) {
								// TODO Auto-generated method stub
								String selectedValue = ((SpinnerClass) xianSpinner
										.getSelectedItem()).getValue();
								unitcode = selectedValue;
								shequTableRow.setVisibility(View.GONE);
								xiaoquTableRow.setVisibility(View.GONE);
								louTableRow.setVisibility(View.GONE);
								spinnerLocation = "xian";
								if (spinnerLocation.equals("xian")) {
									// 选择县填充街道
									jiedaoTableRow.setVisibility(View.VISIBLE);
								} else {
									jiedaoTableRow.setVisibility(View.GONE);
								}
								// 填充街道下拉列表数据
								new setjiedaoSpinnerTask().execute();

							}

							@Override
							public void onNothingSelected(AdapterView<?> arg0) {
								// TODO Auto-generated method stub

							}
						});

				shiSpinner
						.setOnItemSelectedListener(new OnItemSelectedListener() {

							@Override
							public void onItemSelected(AdapterView<?> parent,
									View view, int position, long id) {
								// TODO Auto-generated method stub
								// String selectedValue = ((SpinnerClass)
								// shiSpinner
								// .getSelectedItem()).getValue();
								// unitcode = selectedValue;
								xianTableRow.setVisibility(View.VISIBLE);
								jiedaoTableRow.setVisibility(View.GONE);
								shequTableRow.setVisibility(View.GONE);
								xiaoquTableRow.setVisibility(View.GONE);
								louTableRow.setVisibility(View.GONE);
								String result = delzero();
								spinnerLocation = "shi";
								// 填充县下拉列表数据
								new setxianSpinnerTask().execute();
							}

							@Override
							public void onNothingSelected(AdapterView<?> arg0) {
								// TODO Auto-generated method stub

							}
						});

				// 默认潍坊市选中
				shiSpinner.setSelection(0, true);
				shiSpinner.setEnabled(false);
				builder.setView(view);
				builder.setTitle("数据筛选");
				builder.setPositiveButton("确定",
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								// 根据搜索条件查询
								String finallyUnitcodeshi = ((SpinnerClass)shiSpinner.getSelectedItem()).getValue();
								String finallyUnitcodexian = ((SpinnerClass)xianSpinner.getSelectedItem()).getValue();
								String finallyUnitcodejiedao = ((SpinnerClass)jiedaoSpinner.getSelectedItem()).getValue();
								String finallyUnitcodeshequ = ((SpinnerClass)shequSpinner.getSelectedItem()).getValue();
								String finallyUnitcodexiaoqu = ((SpinnerClass)xiaoquSpinner.getSelectedItem()).getValue();
								String finallyUnitcodelou = ((SpinnerClass)louSpinner.getSelectedItem()).getValue();
								if(!finallyUnitcodeshi.equals("")){
									//Toast.makeText(CommunityStatisticsKuiwenActivity.this, "市"+finallyUnitcodeshi, Toast.LENGTH_LONG).show();
									unitcode = finallyUnitcodeshi;
								}
								if(!finallyUnitcodexian.equals("")){
									//Toast.makeText(CommunityStatisticsKuiwenActivity.this, "县"+finallyUnitcodexian, Toast.LENGTH_LONG).show();
									unitcode = finallyUnitcodexian;
								}
								if(!finallyUnitcodejiedao.equals("")){
									//Toast.makeText(CommunityStatisticsKuiwenActivity.this, "街道"+finallyUnitcodejiedao, Toast.LENGTH_LONG).show();
									unitcode = finallyUnitcodejiedao;
								}
								if(!finallyUnitcodeshequ.equals("")){
									//Toast.makeText(CommunityStatisticsKuiwenActivity.this, "社区"+finallyUnitcodeshequ, Toast.LENGTH_LONG).show();
									unitcode = finallyUnitcodeshequ;
								}
								if(!finallyUnitcodexiaoqu.equals("")){
									//Toast.makeText(CommunityStatisticsKuiwenActivity.this, "小区"+finallyUnitcodexiaoqu, Toast.LENGTH_LONG).show();
									xiaoqu = finallyUnitcodexiaoqu;
								}
								if(!finallyUnitcodelou.equals("")){
									//Toast.makeText(CommunityStatisticsKuiwenActivity.this, "楼"+finallyUnitcodelou, Toast.LENGTH_LONG).show();
									lou = finallyUnitcodelou;
								}
								//Toast.makeText(CommunityStatisticsKuiwenActivity.this, "unitcode最终"+unitcode, Toast.LENGTH_LONG).show();
								//TODO: 执行检索异步线程
								new setTextViewValueTask().execute();
								dialog.dismiss();
							}
						});
				builder.setNegativeButton("取消",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								dialog.dismiss();
							}
						});
				Dialog dialog = builder.create();
				// 设置不能被[返回键]关闭掉
				dialog.setCancelable(false);
				dialog.show();
			}
		});
	}

	/**
	 * 设置textView的值
	 */
	private class setTextViewValueTask extends AsyncTask<Void, Void, String> {

		protected void onPreExecute() {
			// 设置TextView的值
			Log.e("测试", "pre");
			setTextViewValue();
			Toast.makeText(CommunityStatisticsKuiwenActivity.this, "数据加载中...", Toast.LENGTH_LONG).show();
		}

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			Log.e("测试", "back");
			String resultString = getData();
			String finallyResultString = jsonFormat(resultString);
			return finallyResultString;
		}

		protected void onPostExecute(String finallyResultString) {
			Log.e("测试", "ground");
			if (finallyResultString.equals("success")) {
				// 设置TextView的值
				Log.e("测试", "success");
				Toast.makeText(CommunityStatisticsKuiwenActivity.this, "数据加载完毕！", Toast.LENGTH_LONG).show();
				setTextViewValue();
			}
		}

	}

	/**
	 * 设置TextView的值
	 */
	private void setTextViewValue() {
		// TODO Auto-generated method stub
		// 户籍统计
		zhsTextView.setText(zhsValue);
		// 人口统计
		czrksTextView.setText(czrksValue);
		zzrksTextView.setText(zzrksValue);
		kghrksTextView.setText(kghrksValue);
		jghrksTextView.setText(jghrksValue);
		// 性别
		nanTextView.setText(nanValue);
		nvTextView.setText(nvValue);
		// 文化程度
		bswhTextView.setText(bswhValue);
		sswhTextView.setText(sswhValue);
		yjswhTextView.setText(yjswhValue);
		bkwhTextView.setText(bkwhValue);
		dzwhTextView.setText(dzwhValue);
		gzwhTextView.setText(gzwhValue);
		zzwhTextView.setText(zzwhValue);
		czwhTextView.setText(czwhValue);
		xxwhTextView.setText(xxwhValue);
		qtwhTextView.setText(qtwhValue);
		// 政治面貌
		dyTextView.setText(dyValue);
		ybdyTextView.setText(ybdyValue);
		tyTextView.setText(tyValue);
		qzTextView.setText(qzValue);
		mzdTextView.setText(mzdValue);
		// 婚姻状况
		dsTextView.setText(dsValue);
		yhTextView.setText(yhValue);
		lyTextView.setText(lyValue);
		soTextView.setText(soValue);
		// 其他
		jhsyTextView.setText(jhsyValue);
		xgTextView.setText(xgValue);
		dbTextView.setText(dbValue);
		cjTextView.setText(cjValue);
		ltxTextView.setText(ltxValue);
		ftjrTextView.setText(ftjrValue);
		pkhTextView.setText(pkhValue);
		gglrTextView.setText(gglrValue);
		ldrkTextView.setText(ldrkValue);
		ylbxTextView.setText(ylbxValue);
	}

	/**
	 * 跳转到mainmenu页面
	 */
	private void skipMainMenuActivity() {
		Intent mainmenuIntent = new Intent();
		mainmenuIntent.setClass(CommunityStatisticsKuiwenActivity.this,
				Mainmenu.class);
		// 如果前面的activity 没有主动finish 推到栈顶，不然画面会刷一下
		// mainmenuIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		CommunityStatisticsKuiwenActivity.this.startActivity(mainmenuIntent);
		CommunityStatisticsKuiwenActivity.this.finish();
	}

	/**
	 * 重写keydown事件
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			skipMainMenuActivity();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * 计算unitcode级别
	 * 
	 * @return
	 */
	public String delzero()// 删除双0 结尾
	{
		String result = "";
		if (myUnitcode.substring(4).equals("000000")) { // 市
			result = "shi";
		} else if (myUnitcode.substring(6).equals("0000")) { // 县
			result = "xian";
		} else if (myUnitcode.substring(8).equals("00")) { // 街道
			result = "jiedao";
		} else { // 社区
			result = "shequ";
		}
		return result;
	}

	/**
	 * 填充县下拉数据
	 */
	private class setxianSpinnerTask extends AsyncTask<Void, Void, String> {
		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			unitcode = ((SpinnerClass) shiSpinner.getSelectedItem()).getValue();
			String resultString = getSpinnerData("xian");
			String finallyResultString = jsonFormatSpinner(resultString, "xian");
			return finallyResultString;
		}

		protected void onPostExecute(String finallyResultString) {
			if (finallyResultString.equals("success")) {
				// 初始化同时删除其他选项，只能显示自己和下级的数据。
				int len = xianSpinnerArrayList.size();
				for (int i = 0; i < len; i++) {
					SpinnerClass xianSpinnerClass = xianSpinnerArrayList.get(i);
					if (xianSpinnerClass.getValue().equals(xianUnitcode)) {
						xianSpinner.setSelection(i, true);
						xianSpinner.setEnabled(false);
						break;
					}
				}
				xianSpinnerAdapter.notifyDataSetChanged();
			}
		}
	}

	/**
	 * 填充街道下拉数据
	 */
	private class setjiedaoSpinnerTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			unitcode = ((SpinnerClass) xianSpinner.getSelectedItem())
					.getValue();
			String resultString = getSpinnerData("jiedao");
			String finallyResultString = jsonFormatSpinner(resultString,
					"jiedao");
			return finallyResultString;
		}

		protected void onPostExecute(String finallyResultString) {
			if (finallyResultString.equals("success")) {
				// 初始化同时删除其他选项，只能显示自己和下级的数据。
				int len = jiedaoSpinnerArrayList.size();
				for (int i = 0; i < len; i++) {
					SpinnerClass jiedaoSpinnerClass = jiedaoSpinnerArrayList
							.get(i);
					if (jiedaoSpinnerClass.getValue().equals(jiedaoUnitcode)) {
						jiedaoSpinner.setSelection(i, true);
						jiedaoSpinner.setEnabled(false);
					}
				}
				jiedaoSpinnerAdapter.notifyDataSetChanged();
			}
		}
	}

	/**
	 * 填充社区下拉数据
	 */
	private class setshequSpinnerTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			unitcode = ((SpinnerClass) jiedaoSpinner.getSelectedItem())
					.getValue();
			String resultString = getSpinnerData("shequ");
			String finallyResultString = jsonFormatSpinner(resultString,
					"shequ");
			return finallyResultString;
		}

		protected void onPostExecute(String finallyResultString) {
			if (finallyResultString.equals("success")) {
				// 初始化同时删除其他选项，只能显示自己和下级的数据。
				int len = shequSpinnerArrayList.size();
				for (int i = 0; i < len; i++) {
					SpinnerClass shequSpinnerClass = shequSpinnerArrayList
							.get(i);
					if (shequSpinnerClass.getValue().equals(shequUnitcode)) {
						shequSpinner.setSelection(i, true);
						shequSpinner.setEnabled(false);
					}
				}
				shequSpinnerAdapter.notifyDataSetChanged();
			}
		}

	}

	/**
	 * 填充小区下拉数据
	 */
	private class setxiaoquSpinnerTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			unitcode = ((SpinnerClass) shequSpinner.getSelectedItem())
					.getValue();
			String resultString = getSpinnerData("xiaoqu");
			String finallyResultString = jsonFormatSpinner(resultString,
					"xiaoqu");
			return finallyResultString;
		}

		protected void onPostExecute(String finallyResultString) {
			if (finallyResultString.equals("success")) {
				xiaoquSpinnerAdapter.notifyDataSetChanged();
			}
		}

	}

	/**
	 * 填充楼下拉数据
	 */
	private class setlouSpinnerTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			String resultString = getSpinnerData("lou");
			String finallyResultString = jsonFormatSpinner(resultString, "lou");
			return finallyResultString;
		}

		protected void onPostExecute(String finallyResultString) {
			if (finallyResultString.equals("success")) {
				louSpinnerAdapter.notifyDataSetChanged();
			}
		}

	}

	/**
	 * 得到详细信息 ,
	 * 
	 * @return
	 */
	private String getSpinnerData(String jibie) {
		// TODO Auto-generated method stub
		String resultString = "";
		if (jibie.equals("lou")) {
			methodName = "get_LouByXiaoqu";
			soapAcion = "http://tempuri.org/get_LouByXiaoqu";
		} else if (jibie.equals("xiaoqu")) {
			methodName = "get_XiaoquByShequ";
			soapAcion = "http://tempuri.org/get_XiaoquByShequ";
		} else {
			methodName = "GetUnit";
			soapAcion = "http://tempuri.org/GetUnit";
		}

		SoapObject soapObject = new SoapObject(NAMESPACE, methodName);
		// 操作人员所属区域编码
		if (!jibie.equals("xiaoqu") && !jibie.equals("lou")) {
			soapObject.addProperty("type", "0");
		}
		// 这个webservice写成了unitCode，日后改为unticode
		if (jibie.equals("xiaoqu")) {
			soapObject.addProperty("unitCode", unitcode);
		} else {
			soapObject.addProperty("unitcode", unitcode);
		}
		if (jibie.equals("lou")) {
			soapObject
					.addProperty("xiaoqu", ((SpinnerClass) xiaoquSpinner
							.getSelectedItem()).getValue());
		}
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.setOutputSoapObject(soapObject);
		envelope.dotNet = true;
		HttpTransportSE transportSE = new HttpTransportSE(ENDPOINT);
		try {
			// 必须要判断下 ，因为让TableRow 显示，就相当于控件初始化一次？默认选中第一个
			if (!unitcode.equals("")) {
				transportSE.call(soapAcion, envelope);
				if (envelope.getResponse() != null) {
					SoapObject object = (SoapObject) envelope.bodyIn;
					resultString = object.getProperty(0).toString();
					Log.e("webservice取得" + jibie, resultString);
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("IOException", e.getMessage());
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("XmlPullParserException", e.getMessage());
		}
		return resultString;
	}

	/**
	 * 解析json数据返回hashmap格式数据
	 * 
	 * @return
	 */
	private String jsonFormatSpinner(String resultString, String jibie) {
		JSONArray jsonArray;
		try {
			jsonArray = new JSONArray(resultString);
			int len = jsonArray.length();
			JSONObject jsonObject = jsonArray.optJSONObject(0);
			if (jsonObject.optString("Code").equals("1")) {
				if (jibie.equals("xian")) {
					xianSpinnerArrayList.clear();
					xianSpinnerArrayList.add(new SpinnerClass("请选择", ""));
					for (int i = 1; i < len; i++) {
						// 根据实际从第2组开始取得有关数据
						jsonObject = jsonArray.optJSONObject(i);
						xianSpinnerArrayList.add(new SpinnerClass(jsonObject
								.optString("unitname"), jsonObject
								.optString("unitcode")));
					}
				} else if (jibie.equals("jiedao")) {
					jiedaoSpinnerArrayList.clear();
					jiedaoSpinnerArrayList.add(new SpinnerClass("请选择", ""));
					for (int i = 1; i < len; i++) {
						// 根据实际从第2组开始取得有关数据
						jsonObject = jsonArray.optJSONObject(i);
						jiedaoSpinnerArrayList.add(new SpinnerClass(jsonObject
								.optString("unitname"), jsonObject
								.optString("unitcode")));
					}
				} else if (jibie.equals("shequ")) {
					shequSpinnerArrayList.clear();
					shequSpinnerArrayList.add(new SpinnerClass("请选择", ""));
					for (int i = 1; i < len; i++) {
						// 根据实际从第2组开始取得有关数据
						jsonObject = jsonArray.optJSONObject(i);
						shequSpinnerArrayList.add(new SpinnerClass(jsonObject
								.optString("unitname"), jsonObject
								.optString("unitcode")));
					}
				} else if (jibie.equals("xiaoqu")) {
					xiaoquSpinnerArrayList.clear();
					xiaoquSpinnerArrayList.add(new SpinnerClass("请选择", ""));
					for (int i = 1; i < len; i++) {
						// 根据实际从第2组开始取得有关数据
						jsonObject = jsonArray.optJSONObject(i);
						// 小区返回的unitcode，name, 这里都用name
						xiaoquSpinnerArrayList.add(new SpinnerClass(jsonObject
								.optString("name"), jsonObject
								.optString("name")));
					}
				} else if (jibie.equals("lou")) {
					louSpinnerArrayList.clear();
					louSpinnerArrayList.add(new SpinnerClass("请选择", ""));
					for (int i = 1; i < len; i++) {
						// 根据实际从第2组开始取得有关数据
						jsonObject = jsonArray.optJSONObject(i);
						louSpinnerArrayList.add(new SpinnerClass(jsonObject
								.optString("Lou"), jsonObject
								.optString("Lou")));
					}
				}
				return "success";
			} else {
				return "fail";
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "fail";
	}

	/**
	 * 填充楼下拉数据
	 */
	private class getMyUnitcodeAboutTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			String resultString = getMyUnitcodeAboutData();
			String finallyResultString = jsonFormatMyUnitcodeAbout(resultString);
			return finallyResultString;
		}

		protected void onPostExecute(String finallyResultString) {
			if (finallyResultString.equals("success")) {
				Log.e("几个unitcode", "成功得到");
			}
		}

	}

	/**
	 * 得到几个unitcode 相关数据
	 * 
	 * @return
	 */
	private String getMyUnitcodeAboutData() {
		// TODO Auto-generated method stub
		String resultString = "";
		methodName = "GetUnit";
		soapAcion = "http://tempuri.org/GetUnit";
		SoapObject soapObject = new SoapObject(NAMESPACE, methodName);
		// 操作人员所属区域编码
		soapObject.addProperty("type", "2");
		soapObject.addProperty("unitcode", myUnitcode);// 使用MyUnitcode得到上级unitcode
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.setOutputSoapObject(soapObject);
		envelope.dotNet = true;
		HttpTransportSE transportSE = new HttpTransportSE(ENDPOINT);
		try {
			transportSE.call(soapAcion, envelope);
			if (envelope.getResponse() != null) {
				SoapObject object = (SoapObject) envelope.bodyIn;
				resultString = object.getProperty(0).toString();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("IOException", e.getMessage());
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("XmlPullParserException", e.getMessage());
		}
		return resultString;
	}

	/**
	 * 解析json数据
	 * 
	 * @return
	 */
	private String jsonFormatMyUnitcodeAbout(String resultString) {
		JSONArray jsonArray;
		try {
			jsonArray = new JSONArray(resultString);
			int len = jsonArray.length();
			JSONObject jsonObject = jsonArray.optJSONObject(0);
			if (jsonObject.optString("Code").equals("1")) {
				// 根据实际从第2组开始取得有关数据 最多一共5组
				if (len >= 5) {
					jsonObject = jsonArray.optJSONObject(1);
					shiUnitcode = jsonObject.optString("unitcode");
					jsonObject = jsonArray.optJSONObject(2);
					xianUnitcode = jsonObject.optString("unitcode");
					jsonObject = jsonArray.optJSONObject(3);
					jiedaoUnitcode = jsonObject.optString("unitcode");
					jsonObject = jsonArray.optJSONObject(4);
					shequUnitcode = jsonObject.optString("unitcode");
				} else if (len >= 4) {
					jsonObject = jsonArray.optJSONObject(1);
					shiUnitcode = jsonObject.optString("unitcode");
					jsonObject = jsonArray.optJSONObject(2);
					xianUnitcode = jsonObject.optString("unitcode");
					jsonObject = jsonArray.optJSONObject(3);
					jiedaoUnitcode = jsonObject.optString("unitcode");
				} else if (len >= 3) {
					jsonObject = jsonArray.optJSONObject(1);
					shiUnitcode = jsonObject.optString("unitcode");
					jsonObject = jsonArray.optJSONObject(2);
					xianUnitcode = jsonObject.optString("unitcode");
				} else if (len >= 2) {
					jsonObject = jsonArray.optJSONObject(1);
					shiUnitcode = jsonObject.optString("unitcode");
				}
				return "success";
			} else {
				return "fail";
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "fail";
	}

}
